package weaver.interfaces.zhlx.duss.action.hrmresourcemodule.hcm;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.HcmUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-28
 * @Description: 新员工入职定薪及试用期调薪单流程调用HCM定调薪与津贴申请接口
 * @ModifyLog:
 **/
public class WeaXygrzdxjsyqtxdlcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaXygrzdxjsyqtxdlcAction>>>>>requestid=" + requestid);
        try {
            String message = "调用HCM系统接口返回:";
            RecordSet rs = new RecordSet();
            HcmUtil hcmUtil = new HcmUtil();
            ResourceComInfo comInfo = new ResourceComInfo();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String sfydbl = Util.null2String(rs.getString("sfydbl"));
            JSONArray dataList = new JSONArray();
            String mainid = Util.null2String(rs.getString("id"));
            String sqrgh = Util.null2String(rs.getString("sqrgh"));
            String fqr = Util.null2String(rs.getString("fqr"));
            String fqrwb = comInfo.getWorkcode(fqr) + comInfo.getLastname(fqr);
            String tjrq = hcmUtil.formatDate(Util.null2String(rs.getString("tjrq")));
            String dxsxrq = hcmUtil.formatDate(Util.null2String(rs.getString("dxsxrq")));
            String dxyy = Util.null2String(rs.getString("dxyy"));
            String gzdjlx = Util.null2String(rs.getString("gzdjlx"));
            String gzdjfw = Util.null2String(rs.getString("gzdjfw"));
            String jb = Util.null2String(rs.getString("jb"));
            String dcwb = Util.null2String(rs.getString("dcwb"));
            String bzwb1 = Util.null2String(rs.getString("bzwb1"));
            String gzx1 = Util.null2String(rs.getString("gzx1"));
            String dzh1 = Util.null2String(rs.getString("dzh1"));
            String gzx2 = Util.null2String(rs.getString("gzx2"));
            String dzh2 = Util.null2String(rs.getString("dzh2"));
            String dxsxrq1 = Util.null2String(rs.getString("dxsxrq1"));
            String dxyy1 = Util.null2String(rs.getString("dxyy1"));
            String gzdjlx1 = Util.null2String(rs.getString("gzdjlx1"));
            String gzdjfw1 = Util.null2String(rs.getString("gzdjfw1"));
            String jb1 = Util.null2String(rs.getString("jb1"));
            String dcwb1 = Util.null2String(rs.getString("dcwb1"));
            String bzwb2 = Util.null2String(rs.getString("bzwb2"));
            String gzx3 = Util.null2String(rs.getString("gzx3"));
            String dzh3 = Util.null2String(rs.getString("dzh3"));
            String gzx4 = Util.null2String(rs.getString("gzx4"));
            String dzh4 = Util.null2String(rs.getString("dzh4"));
            String jssj = Util.null2String(rs.getString("jssj"));

            JSONObject dataJson = new JSONObject();
            dataJson.put("PERNR", sqrgh);
            dataJson.put("BEGDA", dxsxrq);
            dataJson.put("ENDDA", "99991231");
            dataJson.put("PREAS", dxyy);
            dataJson.put("TRFAR", gzdjlx);
            dataJson.put("TRFGB", gzdjfw);
            dataJson.put("TRFGR", jb);
            dataJson.put("TRFST", dcwb);
            dataJson.put("WAERS", bzwb1);
            dataJson.put("LGA01", gzx1);
            dataJson.put("BET01", dzh1);
            dataJson.put("LGA02", gzx2);
            dataJson.put("BET02", dzh2);
            dataJson.put("ZHR_GUID", requestid);
            dataJson.put("ZHR_LCTJR", fqrwb);
            dataJson.put("ZHR_XGRQ", tjrq);
            JSONArray P0014List = new JSONArray();
            rs.executeQuery("select * from " + tablename + "_dt2 where mainid=?", mainid);
            while (rs.next()) {
                JSONObject P0014Json = new JSONObject();
                P0014Json.put("PERNR", sqrgh);
                P0014Json.put("BEGDA", hcmUtil.formatDate(dxsxrq));
                P0014Json.put("ENDDA", "99991231");
                P0014Json.put("LGART", Util.null2String(rs.getString("gzx")));
                P0014Json.put("ANZHL", Util.null2String(rs.getString("sz")));
                P0014Json.put("ZHR_GUID", requestid);
                P0014Json.put("ZHR_LCTJR", fqrwb);
                P0014Json.put("ZHR_XGRQ", tjrq);
                P0014Json.put("ACTION", Util.null2String(rs.getString("dzzt")));
                P0014List.add(P0014Json);
            }
            dataJson.put("P0014", P0014List);
            dataList.add(dataJson);
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("jkdymc", "WeaXygrzdxjsyqtxdlcAction");
            dataMap.put("lcid", requestid);
            dataMap.put("dylc", requestid);
            dataMap.put("cz", src);
            writeLog(">>>>>WeaXygrzdxjsyqtxdlcAction>>>>>dataList=" + dataList);
            JSONObject resultJson = hcmUtil.handleOKHttpRequest("EC204", dataList, dataMap);
            writeLog(">>>>>WeaXygrzdxjsyqtxdlcAction>>>>>resultJson=" + resultJson);
            String code = Util.null2String(resultJson.getString("code"));
            if ("success".equals(code)) {
                String output = Util.null2String(resultJson.getString("OUTPUT"));
                JSONObject outputJson = JSONObject.parseObject(output);
                String msgType = outputJson.getString("MSG_TYPE");
                if (!"S".equals(msgType)) {
                    message += outputJson.getString("MSG_TEXT");
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            } else {
                message += Util.null2String(resultJson.getString("message"));
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }
            if ("0".equals(sfydbl)) {
                JSONArray dataList2 = new JSONArray();
                JSONObject dataJson2 = new JSONObject();
                dataJson2.put("PERNR", sqrgh);
                dataJson2.put("BEGDA", hcmUtil.formatDate(jssj));
                dataJson2.put("ENDDA", "99991231");
                dataJson2.put("PREAS", "02");
                dataJson2.put("ZHR_GUID", requestid);
                dataJson2.put("ZHR_LCTJR", fqrwb);
                dataJson2.put("ZHR_XGRQ", hcmUtil.formatDate(tjrq));
                dataList2.add(dataJson2);
                writeLog(">>>>>WeaXygrzdxjsyqtxdlcAction>>>>>dataList2=" + dataList2);
                resultJson = hcmUtil.handleOKHttpRequest("EC204", dataList2, dataMap);
                writeLog(">>>>>WeaXygrzdxjsyqtxdlcAction>>>>>resultJson=" + resultJson);
                code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    String output = Util.null2String(resultJson.getString("OUTPUT"));
                    JSONObject outputJson = JSONObject.parseObject(output);
                    String msgType = outputJson.getString("MSG_TYPE");
                    if (!"S".equals(msgType)) {
                        message += outputJson.getString("MSG_TEXT");
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                } else {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            if ("2".equals(sfydbl)) {
                JSONArray dataList1 = new JSONArray();
                JSONObject dataJson1 = new JSONObject();
                dataJson1.put("PERNR", sqrgh);
                dataJson1.put("BEGDA", hcmUtil.formatDate(dxsxrq1));
                dataJson1.put("ENDDA", "99991231");
                dataJson1.put("PREAS", dxyy1);
                dataJson1.put("TRFAR", gzdjlx1);
                dataJson1.put("TRFGB", gzdjfw1);
                dataJson1.put("TRFGR", jb1);
                dataJson1.put("TRFST", dcwb1);
                dataJson1.put("WAERS", bzwb2);
                dataJson1.put("LGA01", gzx3);
                dataJson1.put("BET01", dzh3);
                dataJson1.put("LGA02", gzx4);
                dataJson1.put("BET02", dzh4);
                dataJson1.put("ZHR_GUID", requestid);
                dataJson1.put("ZHR_LCTJR", fqrwb);
                dataJson1.put("ZHR_XGRQ", hcmUtil.formatDate(tjrq));
                JSONArray P0014List1 = new JSONArray();
                rs.executeQuery("select * from " + tablename + "_dt4 where mainid=?", mainid);
                while (rs.next()) {
                    JSONObject P0014Json = new JSONObject();
                    P0014Json.put("PERNR", sqrgh);
                    P0014Json.put("BEGDA", hcmUtil.formatDate(dxsxrq1));
                    P0014Json.put("ENDDA", "99991231");
                    P0014Json.put("LGART", Util.null2String(rs.getString("gzx")));
                    P0014Json.put("ANZHL", Util.null2String(rs.getString("sz")));
                    P0014Json.put("ZHR_GUID", requestid);
                    P0014Json.put("ZHR_LCTJR", fqrwb);
                    P0014Json.put("ZHR_XGRQ", tjrq);
                    P0014Json.put("ACTION", Util.null2String(rs.getString("dzzt")));
                    P0014List1.add(P0014Json);
                }
                dataJson1.put("P0014", P0014List1);
                dataList1.add(dataJson1);
                writeLog(">>>>>WeaXygrzdxjsyqtxdlcAction>>>>>dataList1=" + dataList1);
                resultJson = hcmUtil.handleOKHttpRequest("EC204", dataList1, dataMap);
                writeLog(">>>>>WeaXygrzdxjsyqtxdlcAction>>>>>resultJson=" + resultJson);
                code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    String output = Util.null2String(resultJson.getString("OUTPUT"));
                    JSONObject outputJson = JSONObject.parseObject(output);
                    String msgType = outputJson.getString("MSG_TYPE");
                    if (!"S".equals(msgType)) {
                        message += outputJson.getString("MSG_TEXT");
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                } else {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaXygrzdxjsyqtxdlcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}